package com.fw.controller;

import com.alibaba.fastjson.JSONObject;
import com.fw.entity.*;
import com.fw.service.*;
import org.hibernate.validator.constraints.pl.REGON;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class TraceController {
    @Resource
    IMeatService meatService;
    @Resource
    IPigService pigService;
    @Resource
    IUserService userService;
    @Resource
    IPigorderService pigorderService;
    @Resource
    IMorderService morderService;
    @Resource
    IWholesalersService wholesalersService;
    @RequestMapping("/trace")
    public Result Trace(@RequestParam String mid) throws MyException {
        if (mid.equals(""))
            throw new MyException("猪肉id不能为空", "070101");
        Integer id = Integer.parseInt(mid);
        JSONObject result = new JSONObject();
        Meat meat = meatService.getById(mid);
        if(meat == null)
            throw new MyException("该猪肉id不存在", "070102");
        Integer mpath1 = meat.getMpath1();
        Integer mpath2 = meat.getMpath2();
        Integer mpath3 = meat.getMpath3();
        Integer mpath4 = meat.getMpath4();
        Pig pig = pigService.getById(mpath1);
        //获取不同的内容存入不同的json
        JSONObject temp1 = new JSONObject();
        JSONObject temp2 = new JSONObject();
        JSONObject temp3 = new JSONObject();
        JSONObject temp4 = new JSONObject();
        Pigorder pigorder = pigorderService.getById(pig.getPigoid());//通过猪肉id获取订单
        //养殖户
        temp1.put("uid", pig.getPigraiseuid());//养殖场id
        temp1.put("uname", userService.getById(pig.getPigraiseuid()).getUname());//养殖场name
        temp1.put("outdate", pigorder.getOutdate());//生猪出场日期
        temp1.put("pigraisecheck", pig.getPigraisecheck());//养殖场检疫
        temp1.put("pigid", pig.getPigid());//生猪id
        Morder morder1 = morderService.getById(mpath2);
        //屠宰场
        temp2.put("uid", morder1.getOselluid());//屠宰场uid
        temp2.put("uname", userService.getById(morder1.getOselluid()).getUname());//屠宰场name
        temp2.put("pigindate", pigorder.getIndate());//生猪的进场日期
        temp2.put("meatoutdate", morder1.getOutdate());//猪肉的出厂日期
        temp2.put("pigkillcheck", pig.getPigkillcheck());//屠宰场对生猪检疫
        //批发市场
        temp3.put("uid", morder1.getObuyuid());//批发市场id
        temp3.put("uname", userService.getById(morder1.getObuyuid()).getUname());//批发市场名称
        temp3.put("meatindate", morder1.getIndate());//猪肉进入批发市场时间
        Morder morder2;
        if(mpath4 != null) morder2 = morderService.getById(mpath4);
        else morder2 = new Morder();
        temp3.put("meatoutdate", morder2.getOutdate());
        if(mpath3 != null)
            temp3.put("wholesaler", wholesalersService.getById(mpath3).getWname());
        else temp3.put("wholesaler", "");
        //零售商
        if(mpath4 != null) {
            temp4.put("uid", morder2.getObuyuid());
            temp4.put("uname", userService.getById((morder2.getObuyuid())).getUname());
            temp4.put("meatindate", morder2.getIndate());
        }else{
            temp4.put("uid", "");
            temp4.put("uname", "");
            temp4.put("meatindate", "");
        }
        result.put("feeder", temp1);
        result.put("slaughterhouse", temp2);
        result.put("wholesaler", temp3);
        result.put("retailer", temp4);
        return new Result(result, "返回溯源信息成功", "000000");
    }
}
